package com.zimi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zimi.mapper.WfSystaskMapper;
import com.zimi.model.*;
import com.zimi.service.WfSystaskKeyService;
import com.zimi.service.WfSystaskService;
import com.zimi.service.WfTaskService;
import com.zimi.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("流程实例")
@RestController
@RequestMapping("wf/systask/key")
public class WfSystaskKeyApi {

    @Autowired
    private WfSystaskKeyService service;
    @Autowired
    private WfTaskService wfTaskService;
    @Autowired
    private WfSystaskService wfSystaskService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private WfSystaskMapper wfSystaskMapper;


    @PostMapping("pageList")
    @ApiOperation("分页查询")
    public Object pageList(WfSystaskKey record) {
        PageHelper.startPage(record.getPage(), record.getLimit());
        List<WfSystaskKey> list = service.selectAll(record);
        PageInfo<WfSystaskKey> pageInfo = new PageInfo<>(list);
        Map map = new HashMap();
        map.put("code", 200);
        map.put("total", pageInfo.getTotal());
        map.put("rows", list);
        return map;
    }

    @PostMapping("pageMyDoneTask")
    @ApiOperation("我的审批")
    public Object pageMyDoneTask(WfSystaskKey record) {
        PageHelper.startPage(record.getPage(), record.getLimit());
        List<WfSystaskKeyVO> list = service.selectMyDoneTask(record);
        PageInfo<WfSystaskKeyVO> pageInfo = new PageInfo<>(list);
        Map map = new HashMap();
        map.put("code", 200);
        map.put("total", pageInfo.getTotal());
        map.put("rows", list);
        return map;
    }

    @PostMapping("list")
    @ApiOperation("查询所有")
    public List<WfSystaskKey> selectAll(WfSystaskKey record) {
        return service.selectAll(record);
    }

    @PostMapping("{id}")
    @ApiOperation("根据Id查询信息")
    public WfSystaskKey selectByPrimaryKey(@PathVariable("id") int id) {
        return service.selectByPrimaryKey(id);
    }

    @PostMapping("del/{id}")
    @ApiOperation("根据Id删除")
    public boolean deleteByPrimaryKey(@PathVariable("id") int id) {
        return service.deleteByPrimaryKey(id) > 0 ? true : false;
    }

    @PostMapping("add")
    @ApiOperation("添加")
    public boolean insert(WfSystaskKey record) {
        return service.insertSelective(record) > 0 ? true : false;
    }

    @PostMapping("update")
    @ApiOperation("根据条件修改信息")
    public boolean updateByPrimaryKey(WfSystaskKey record) {
        return service.updateByPrimaryKeySelective(record) > 0 ? true : false;
    }

    @PostMapping("submit")
    @ApiOperation("流程提交")
    public Map submitTask() {
        Map map = new HashMap();
        try {
            String[] ma = request.getParameterValues("recieverInfo");
            Gson gson = new Gson();
            WfTaskTest taskTest = gson.fromJson(ma[0], new TypeToken<WfTaskTest>() {
            }.getType());
            //控制代办不能多次提交
            if (taskTest.getWfTaskId() != null && !"".equals(taskTest.getWfTaskId())) {
                WfTask wfTask = new WfTask();
                wfTask = wfTaskService.selectByPrimaryKey(Integer.valueOf(taskTest.getWfTaskId()));
                if (wfTask.getWfTaskState() == 2) {
                    map.put("wfState", "over");
                    return map;
                }
            }
            List<WfTargetActivityAndOperators> wfTargetList = service.saveSubmit(taskTest);
            if (wfTargetList.size() > 1) {
                //大于一个活动返回拿到的活动然用户选择提交给谁
                map.put("wfState", "DUO");
                List<WfActivity> wfActivities = new ArrayList();
                for (int i = 0; i < wfTargetList.size(); i++) {
                    if (wfTargetList.get(i).getOperatorList() != null && wfTargetList.get(i).getOperatorList().size() != 0 || "end".equals(wfTargetList.get(i).getWfActivity().getActivityCode())) {
                        wfActivities.add(wfTargetList.get(i).getWfActivity());
                    }
                }
                map.put("wfTargetList", wfActivities);
            } else if (wfTargetList.size() == 1) {
                //返回拿到的活动 让用户知道下一活动情况
                if (wfTargetList.get(0).getOperatorList() != null && wfTargetList.get(0).getOperatorList().size() != 0) {
                    if ("bin".equals(wfTargetList.get(0).getOperatorList().get(0))) {
                        map.put("wfState", "bin");
                    } else if ("chuan".equals(wfTargetList.get(0).getOperatorList().get(0))) {
                        map.put("wfState", "chuan");
                        map.put("operatorList", wfTargetList.get(0).getOperatorList());
                    } else {
                        map.put("operatorList", wfTargetList.get(0).getOperatorList());
                        map.put("wfActivity", wfTargetList.get(0).getWfActivity());
                    }
                } else if ("end".equals(wfTargetList.get(0).getWfActivity().getActivityCode())) {
                    map.put("wfActivity", wfTargetList.get(0).getWfActivity());
                } else {
                    map.put("user", "null");
                }
            } else if (wfTargetList.size() == 0) {
                // 没有拿到活动返回除开始  当前活动外的所有活动让用户选择
                map.put("wfState", "NO");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("OK", "error");
            map.put("message", e.getMessage());
            return map;
        }
        map.put("OK", "OK");
        return map;
    }

    @PostMapping("save")
    @ApiOperation("流程保存")
    public Map saveTask() {
        Map map = new HashMap();
        try {
            String[] ma = request.getParameterValues("recieverInfo");
            Gson gson = new Gson();
            WfTaskTest taskTest = gson.fromJson(ma[0], new TypeToken<WfTaskTest>() {
            }.getType());
            map = service.saveSave(taskTest);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("OK", "error");
            map.put("message", e.getMessage());
            return map;
        }
        map.put("OK", "OK");
        return map;
    }

    @PostMapping("goback")
    @ApiOperation("流程退回")
    public Map goBack() {
        Map map = new HashMap();
        try {
            String[] ma = request.getParameterValues("recieverInfo");
            Gson gson = new Gson();
            WfTaskTest taskTest = gson.fromJson(ma[0], new TypeToken<WfTaskTest>() {
            }.getType());
            //控制代办不能多次提交
            if (taskTest.getWfTaskId() != null && !"".equals(taskTest.getWfTaskId())) {
                WfTask wfTask = new WfTask();
                wfTask = wfTaskService.selectByPrimaryKey(Integer.valueOf(taskTest.getWfTaskId()));
                if (wfTask.getWfTaskState() == 2) {
                    map.put("wfState", "over");
                    return map;
                }
            }
            List<WfTargetActivityAndOperators> wfTargetList = service.saveGoBack(taskTest);
            map.put("operatorList", wfTargetList.get(0).getOperatorList());
            map.put("wfActivity", wfTargetList.get(0).getWfActivity());
        } catch (Exception e) {
            e.printStackTrace();
            map.put("OK", "error");
            map.put("message", e.getMessage());
            return map;
        }
        map.put("OK", "OK");
        return map;
    }

    @PostMapping("getoptionty/{id}")
    @ApiOperation("流程抢占")
    public Map getOptionTy(@PathVariable("id") int id) {
        Map map = new HashMap();
        boolean flag = service.delByOptionType(id);
        if (flag) {
            map.put("TYPE", "OK");
        } else {
            map.put("TYPE", "error");
        }
        map.put("OK", "OK");
        return map;
    }

    @PostMapping("savewfnull")
    @ApiOperation("流程异常、跳转提交")
    public Map saveWfNull(int id, int activityId) throws Exception {
        Map map = new HashMap();
        if (id != 0) {
            WfSystask wfSystask = new WfSystask();
            SysUser user = UserUtil.getUser();
            wfSystask = wfSystaskService.selectByPrimaryKey(id);
            WfTask wfTask = new WfTask();
            wfTask.setSysTaskKeyId(wfSystask.getSysTaskKeyId());
            wfTask.setSysTaskId(wfSystask.getId());
            wfTask.setTaskExecutor(user.getId());
            List<WfTask> wfTasks = wfTaskService.selectAll(wfTask);
            if (wfTasks.get(0).getWfTaskState() == 2) {
                map.put("wfState", "over");
                return map;
            }
        }
        List<WfTargetActivityAndOperators> wfTargetList = service.saveWfNull(id, activityId);
        if (wfTargetList.get(0).getOperatorList() != null && wfTargetList.get(0).getOperatorList().size() != 0) {
            if ("bin".equals(wfTargetList.get(0).getOperatorList().get(0))) {
                map.put("wfState", "bin");
            } else if ("chuan".equals(wfTargetList.get(0).getOperatorList().get(0))) {
                map.put("wfState", "chuan");
                map.put("operatorList", wfTargetList.get(0).getOperatorList());
            } else {
                map.put("operatorList", wfTargetList.get(0).getOperatorList());
                map.put("wfActivity", wfTargetList.get(0).getWfActivity());
            }
        } else if ("end".equals(wfTargetList.get(0).getWfActivity().getActivityCode())) {
            map.put("wfActivity", wfTargetList.get(0).getWfActivity());
        } else {
            map.put("user", "null");
        }

        map.put("OK", "OK");
        return map;
    }

    @PostMapping("revoke")
    @ApiOperation("流程撤销")
    public Map revokeTask() throws Exception {
        Map map = new HashMap();
        String[] ma = request.getParameterValues("recieverInfo");
        Gson gson = new Gson();
        WfTaskTest taskTest = gson.fromJson(ma[0], new TypeToken<WfTaskTest>() {
        }.getType());
        //控制代办不能多次提交
        if (taskTest.getWfTaskId() != null && !"".equals(taskTest.getWfTaskId())) {
            WfTask wfTask = new WfTask();
            wfTask = wfTaskService.selectByPrimaryKey(Integer.valueOf(taskTest.getWfTaskId()));
            if (wfTask.getWfTaskState() == 2) {
                map.put("wfState", "over");
                return map;
            }
        }
        service.saveRevoke(taskTest);
        map.put("OK", "OK");
        return map;

    }

    @PostMapping("pageListVO")
    @ApiOperation("流程实例列表")
    @ResponseBody
    public Map pageListVO(WfSystaskKeyVO wfSystaskKeyVO) {
        PageHelper.startPage(wfSystaskKeyVO.page, wfSystaskKeyVO.limit);
        List<WfSystaskKeyVO> list = service.selectAllVO(wfSystaskKeyVO);
        PageInfo<WfSystaskKey> pageInfo = new PageInfo<WfSystaskKey>();
        Map map = new HashMap();
        map.put("code", 200);
        map.put("count", pageInfo.getTotal());
        map.put("rows", list);
        return map;
    }

}
